//
//  FirstViewController.m
//  TUMensa
//
//  Created by Martin Poehlmann on 4/7/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MenuViewController.h"
#import "DailyMenu.h"
#import "MenuCell.h"b

//Private Methods
@interface MenuViewController ()


@end

@implementation MenuViewController
@synthesize table;

@synthesize navigationBar, prev, next;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if (dataProvider == nil)
        dataProvider = [[MenuDataProvider alloc] init];
    
    [dataProvider loadData];
    
    [self updateView];
}

-(void)updateView
{
    DailyMenu *menu = [dataProvider getCurrentMenu];
    self.navigationBar.title = menu.dateString;

    self.prev.enabled = [dataProvider hasPrevItem];
    self.next.enabled = [dataProvider hasNextItem];
    
    [self.table reloadData]; 
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload
{
    [self setTable:nil];
    [super viewDidUnload];
    
    

    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc
{
    [dataProvider release];
    
    [table release];
    [super dealloc];
}

/// table view methods

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    DailyMenu *menu = [dataProvider getCurrentMenu];
    return [menu.meals count]; 
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"Mensa Garching";
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MenuCell";
    MenuCell *cell = (MenuCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        
        NSArray *tlObjects = [[NSBundle mainBundle] loadNibNamed:@"MenuCell" owner:nil options:nil];
        
        for (id currentObject in tlObjects) {
            if ([currentObject isKindOfClass:[MenuCell class]])
            {
                cell = (MenuCell*) currentObject;
                break;
            }
        }
       
//       /* cell = [[[UITableViewCell alloc]
//                 initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
//    */
////        cell = [[[UITableViewCell alloc]
////                 initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
////        
//        
    }
    
    DailyMenu *menu = [dataProvider getCurrentMenu];
    Meal *meal = [menu.meals objectAtIndex:indexPath.row];
    cell.primaryLabel.text = meal.name;
    cell.secondaryLabel.text = meal.price;
    cell.tagsLabel.text = meal.tags;
    
    return cell;
    

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

-(void)onNext:(id)sender
{
    [dataProvider next];
    [self updateView];
}

-(void)onPrev:(id)sender
{
    [dataProvider prev];
    [self updateView];
}

@end;
